<?php
namespace app\controller;
use think\Controller;
use think\Hook;

class Common extends Controller{

  public function _initialize(){
    $current_controller = request()->controller();
    $controler = explode('.',$current_controller)[0];
    if($controler == 'Admin'){
      Hook::listen('check_login');
      Hook::listen('check_permission');
    }elseif(explode('.',$current_controller)[0] == 'Handler'){
      Hook::listen('check_handler');
    }elseif(explode('.',$current_controller)[0] == 'Api'){
//      Hook::listen('check_api');
    }else{
      exit('什么鬼');
    }
  }

  protected $beforeActionList = [
    'is_pjax',
    'updateRefreshTime',
  ];

  //判断是否pjax
  public function is_pjax(){
    if(!(array_key_exists('HTTP_X_PJAX', $_SERVER) && $_SERVER['HTTP_X_PJAX'] === 'true')){
      $this->loadLayout();
    }
  }

  //加载母版
  public function loadLayout(){
    $this->loadSideMenu();
    $this->view->engine->layout('layout/admin');
  }

  //加载侧边栏
  public function loadSideMenu(){
    if(!session('?sideMenu')){
      $json_file = file_get_contents('static/json/sidemenu.json');
      $sideMenu = json_decode($json_file,true);
      session('sideMenu',$sideMenu);
    }
  }

  //重置活跃时间
  public function updateRefreshTime(){
    session('refreshTime',time());
  }

  //空置操作
  public function _empty(){
    return redirect('/admin/index');
  }


}//END
